package com.ruoyi.common.service.impl;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.service.IOrgUserService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrgUserServiceImpl implements IOrgUserService {

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public List<TreeSelect> buildDeptTreeSelect(Long deptId, boolean self) {
        List<TreeSelect> tempTree = new ArrayList<TreeSelect>();
        SysDept sysDept = null;
        if (deptId == null) {
            sysDept = deptMapper.selectRootDeptByParent();
        } else {
            sysDept = deptMapper.selectDeptById(deptId);
        }
        List<SysUser> usres = userMapper.selectUserByDeptId(sysDept.getDeptId());
        List<SysDept> depts = deptMapper.selectAllDeptListByParent(sysDept.getDeptId());
        if (self) {
            tempTree.add(new TreeSelect(sysDept));
            if (usres.size() > 0) {
                if (tempTree.get(0).getChildren() == null) {
                    tempTree.get(0).setChildren(usres.stream().map(TreeSelect::new).collect(Collectors.toList()));
                } else {
                    tempTree.get(0).getChildren().addAll(usres.stream().map(TreeSelect::new).collect(Collectors.toList()));
                }
            }
            if (depts.size() > 0) {
                if (tempTree.get(0).getChildren() == null)
                    tempTree.get(0).setChildren(new ArrayList<TreeSelect>());
                for (SysDept sDept : depts) {
                    TreeSelect xtreeDept = new TreeSelect(sDept);
                    xtreeDept.setChildren(buildDeptTreeSelect(sDept.getDeptId(), false));
                    tempTree.get(0).getChildren().add(xtreeDept);
                }
            }
        } else {
            tempTree.addAll(usres.stream().map(TreeSelect::new).collect(Collectors.toList()));
            if (depts.size() > 0) {
                for (SysDept sDept : depts) {
                    TreeSelect xtreeDept = new TreeSelect(sDept);
                    xtreeDept.setChildren(buildDeptTreeSelect(sDept.getDeptId(), false));
                    tempTree.add(xtreeDept);
                }
            }
        }

        return tempTree;
    }

    @Override
    public List<TreeSelect> selectAllDeptList(Long parentDeptId) {
        return null;
    }

    @Override
    public List<SysUser> selectUserByDeptId(Long deptId) {
        return userMapper.selectUserByDeptId(deptId);
    }

    @Override
    public List<TreeSelect> getOrgForTeam(long deptId) {
        SysDept sysDept = deptMapper.selectDeptById(deptId);
        String[] depts = sysDept.getAncestors().split(",");
        if (depts.length >= 3) {
            //科室
            return buildDeptTreeSelect(Long.parseLong(depts[2]), true);
        } else {
            return null;
        }
    }

    @Override
    public List<TreeSelect> getOrgForDept(long deptId) {
        SysDept sysDept = deptMapper.selectDeptById(deptId);
        String[] depts = sysDept.getAncestors().split(",");
        if (depts.length >= 2) {
            //部门
            return buildDeptTreeSelect(Long.parseLong(depts[1]), true);
        } else {
            return null;
        }
    }

    @Override
    public List<TreeSelect> getOrgTeamManager(long deptId) {
        SysDept sysDept = deptMapper.selectDeptById(deptId);
        String[] depts = sysDept.getAncestors().split(",");
        List<TreeSelect> tempTree = new ArrayList<TreeSelect>();
        if (depts.length >= 3) {
            //室经理
            SysDept tempDept = deptMapper.selectDeptById(Long.parseLong(depts[2]));
            TreeSelect treeSelect=new TreeSelect();
            treeSelect.setId(tempDept.getDeptId());
            treeSelect.setLabel(tempDept.getDeptName());
            treeSelect.setChildren(new ArrayList<TreeSelect>());

            TreeSelect treeUser=new TreeSelect();
            treeUser.setId(Long.parseLong("1"));
            treeUser.setUserName(tempDept.getLeader());
            treeUser.setLabel(tempDept.getLeader_cn());

            treeSelect.getChildren().add(treeUser);
            tempTree.add(treeSelect);

        }
        return tempTree;
    }

    @Override
    public List<TreeSelect> getOrgDeptManager(long deptId) {
        SysDept sysDept = deptMapper.selectDeptById(deptId);
        String[] depts = sysDept.getAncestors().split(",");
        List<TreeSelect> tempTree = new ArrayList<TreeSelect>();
        if (depts.length >= 2) {
            //部门经理
            SysDept tempDept = deptMapper.selectDeptById(Long.parseLong(depts[1]));
            TreeSelect treeSelect=new TreeSelect();
            treeSelect.setId(tempDept.getDeptId());
            treeSelect.setLabel(tempDept.getDeptName());
            treeSelect.setChildren(new ArrayList<TreeSelect>());

            TreeSelect treeUser=new TreeSelect();
            treeUser.setId(Long.parseLong("1"));
            treeUser.setUserName(tempDept.getLeader());
            treeUser.setLabel(tempDept.getLeader_cn());

            treeSelect.getChildren().add(treeUser);
            tempTree.add(treeSelect);

        }
        return tempTree;
    }
}
